package com.bjc.core.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bjc.base.bean.ResultVO;
import com.bjc.base.core.ResultGenerator;
import com.bjc.base.core.annotation.PermissionPath;
import com.bjc.core.bean.request.LookupReqVO;
import com.bjc.core.bean.response.LookupVO;
import com.bjc.core.service.ILookupService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * lookup控制器
 * 
 * @author 许自辉
 * @date 2017年7月5日 下午5:23:15
 */

@SpringBootConfiguration
@RestController
@RequestMapping("api/cloud")
@PermissionPath("系统管理/lookup")
@Api(value = "lookup-api", tags = { "lookup相关接口" })
public class LookupController {

	@Autowired
	private ILookupService lookupService;

	@ApiOperation("分页查询lookup")
	@RequiresPermissions("lookup:findPage")
	@GetMapping("lookup/findPage")
	public ResultVO getLookupPage(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize,
			LookupReqVO lookupReqVO) {
		PageHelper.startPage(pageNum, pageSize);
		List<LookupVO> lookupVOs = lookupService.getList(lookupReqVO);
		return ResultGenerator.getSuccess(new PageInfo<LookupVO>(lookupVOs));
	}

	@ApiOperation("添加lookup")
	@RequiresPermissions("lookup:create")
	@PostMapping("lookup/create")
	public ResultVO createLookup(@RequestBody LookupReqVO lookupReqVOs) {
		int count = lookupService.insert(lookupReqVOs);
		if (count == -1) {
			return ResultGenerator.getFail("该lookup已经存在！");
		}
		return ResultGenerator.getInsertSuccess(count);
	}

	@ApiOperation("更新lookup")
	@RequiresPermissions("lookup:update")
	@PutMapping("lookup/update")
	public ResultVO updateLookup(@RequestBody LookupReqVO lookupReqVOs) {
		int count = lookupService.update(lookupReqVOs);
		return ResultGenerator.getUpdateSuccess(count);
	}

	@ApiOperation("批量删除lookup")
	@RequiresPermissions("lookup:deleteList")
	@DeleteMapping("lookup/deleteList")
	public ResultVO deleteLookup(@ApiParam(value = "lookup id集合") @RequestBody int[] ids) {
		int count = lookupService.deleteList(ids);
		return ResultGenerator.getDeleteSuccess(count);
	}
}
